<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Area;
use App\Services\StoreService;
use Illuminate\Http\Request;
use Rizhou\Control\Supply\StoreSynchronizing;

class AreaController extends Controller
{
    private $request_city_name;
    private $request_county_name;
    private $request_road_name;

    public function __construct(Request $request)
    {
        $this->request_city_name = $request->city_name?trim($request->city_name):"";

        $this->request_county_name = $request->county_name?trim($request->county_name):"";

        $this->request_road_name = $request->road_name?trim($request->road_name):"";
    }

    public function get(Request $request){
        $area = Area::where('parent_id',$request->get('pid',0))->where('is_special',0)->get()->toJson();
        return response()->json($area);
    }

    public function getCity(Request $request){
        if($request->type == 1){
            $data = StoreSynchronizing::make()->getCity();

        }else{
            $data = Area::where('parent_id',0)->where('is_special',0)->select(['id','parent_id as pid','level','name'])->get()->toJson();
        }
        return response()->json($data);
    }

    public function getCounty(Request $request){
        if($request->type == 1){
            $data = StoreSynchronizing::make()->getCounty($this->request_city_name);
        }else{
            $city = Area::where('parent_id',0)->where('is_special',0)->where('name',$this->request_city_name)->select(['id','parent_id as pid','level','name'])->first();
            $data = Area::where('parent_id',$city->id)->where('is_special',0)->select(['id','parent_id as pid','level','name'])->get()->toJson();
        }
        return response()->json($data);
    }

    public function getRoad(Request $request){
        if($request->type == 1){
            $data = StoreSynchronizing::make()->getRoad($this->request_city_name,$this->request_county_name);
        }else{
            $city = Area::where('parent_id',0)->where('is_special',0)->where('name',$this->request_city_name)->select(['id','parent_id as pid','level','name'])->first();
            $county = Area::where('parent_id',$city->id)->where('is_special',0)->where('name',$this->request_county_name)->select(['id','parent_id as pid','level','name'])->first();
            $data = Area::where('parent_id',$county->id)->where('is_special',0)->select(['id','parent_id as pid','level','name'])->get()->toJson();
        }
        return response()->json($data);
    }

    public function getShop(Request $request){
        $data = [];
        if($request->type == 1){
            $data = StoreSynchronizing::make()->getShop($this->request_city_name,$this->request_county_name,$this->request_road_name);
        }
        return response()->json($data);
    }

    public function get711(){

        //StoreSynchronizing::make()->synchro();

        //dd(StoreSynchronizing::make()->getCity());

        //dd(StoreSynchronizing::make()->getCounty('台北市'));

        //dd(StoreSynchronizing::make()->getRoad('台北市','大安區'));

        //dd(StoreSynchronizing::make()->getShop('台北市','大安區','忠孝東路四段'));

        /*dd(app(StoreService::class)->getCity());

        dd(app(StoreService::class)->getCounty('台北市'));

        dd(app(StoreService::class)->getRoad('台北市','大安區'));

        dd(app(StoreService::class)->getShop('台北市','大安區','忠孝東路四段'));*/
    }
}
